a = int(input())
s = a // 1000
d = a // 100 % 10
f = a % 100 //10
g = a % 10
if a <= 999 or a >= 10000:
    print(0)
elif a // 2 != a / 2 and a // 3 != a / 3:
    print(a)
elif (s * 100 + d * 10 + f) // 2 != (s * 100 + d * 10 + f) / 2 and (s * 100 + d * 10 + f) // 3 != (s * 100 + d * 10 + f) / 3:
    print(s * 100 + d * 10 + f)
elif (s * 10 + d) // 2 != (s * 10 + d) / 2 and (s * 10 + d) // 3 != (s * 10 + d) / 3:
    print(s * 10 + d)
elif s // 2 != s / 2 and s // 3 != s / 3 or s == 2 or s != 1:
    print(s)
else:
    print(0)